package Controls
{
	import Layout.GameLayout;
	
	import Objects.GameBitmap;
	
	public class TreeItem extends ItemRender
	{
		public var dropButton:Button;
		public var icon:GameBitmap;
		public var label:Label;
		public var folder:Boolean=false;
		public var unfold:Boolean=false;
		public var parentNote:TreeItem;
		public var iconImages:Array=[];
		public var elderNotes:Array=[];
		public function TreeItem()
		{
			super();
			
			dropButton=new Button();
			addChild(dropButton);
			
			icon=new GameBitmap();
			addChild(icon);
			
			label=new Label();
			addChild(label);
		}
		override protected function $update():void
		{
			super.$update();
			
			var visible:Boolean=true;
			
			for each(var note:TreeItem in elderNotes)
			{
				if(!note.unfold)
				{
					visible=false;
				}
			}
			
			obj.visible=visible;
			
			if(folder)
			{
				if(dropButton.mHit)
				{
					unfold=!unfold;
				}
				
				dropButton.selected=unfold;
				
				if(unfold)
				{
					icon.setImage(iconImages[1]);
				}
				else
				{
					icon.setImage(iconImages[0]);
				}
			}
			else
			{
				icon.setImage(iconImages[2]);
			}
		}
		public function layout(index:int):void
		{
			
		}
		override protected function dataSeted():void
		{
			if(data["value"] is Array)
			{
				folder=true;
			}
			
			if(!folder)
			{
				dropButton.obj.visible=false;
			}
			
			label.textField.text=data["label"];
		}
		public function getElderNotes():Array
		{
			var list:Array=new Array();
			
			var temp:TreeItem=this;
			while(temp.parentNote)
			{
				list.push(temp.parentNote);
				temp=temp.parentNote;
			}
			
			return list;
		}
	}
}